package com.neusoft.zqygbc.neusoft_wenda.activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.view.SmoothImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class SpaceImageDetailActivity extends AppCompatActivity {

    //手指按下的点为(x1, y1)手指离开屏幕的点为(x2, y2)
    float x1 = 0;
    float x2 = 0;
    private Context mContext;
    private int mPosition;
    private ArrayList urllist;
    private SmoothImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        urllist = getIntent().getStringArrayListExtra("images");
        mPosition = getIntent().getIntExtra("position", 0);
        int mLocationX = getIntent().getIntExtra("locationX", 0);
        int mLocationY = getIntent().getIntExtra("locationY", 0);
        int mWidth = getIntent().getIntExtra("width", 0);
        int mHeight = getIntent().getIntExtra("height", 0);

        imageView = new SmoothImageView(this);
        imageView.setOriginalInfo(mWidth, mHeight, mLocationX, mLocationY);
        imageView.transformIn();
        imageView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        setContentView(imageView);
        Picasso.with(mContext).load(urllist.get(mPosition).toString()).into(imageView);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //继承了Activity的onTouchEvent方法，直接监听点击事件
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            //当手指按下的时候
            x1 = event.getX();
        }
        if(event.getAction() == MotionEvent.ACTION_UP) {
            //当手指离开的时候
            x2 = event.getX();
            if(x1 - x2 > 50) {
               // Toast.makeText(SpaceImageDetailActivity.this, "向左滑", Toast.LENGTH_SHORT).show();
                mPosition++;
                if(mPosition<urllist.size()){
                    Picasso.with(mContext).load(urllist.get(mPosition).toString()).into(imageView);
                }else{
                    Toast.makeText(SpaceImageDetailActivity.this, "没有图片了!!!", Toast.LENGTH_SHORT).show();
                }
            } else if(x2 - x1 > 50) {
               // Toast.makeText(SpaceImageDetailActivity.this, "向右滑", Toast.LENGTH_SHORT).show();
                mPosition--;
                if(mPosition>=0){
                    Picasso.with(mContext).load(urllist.get(mPosition).toString()).into(imageView);
                }
            }else{
                finish();
                overridePendingTransition(0,0);

            }
        }
        return super.onTouchEvent(event);
    }
}
